package com.southwind.controller;


import com.southwind.common.Cache;
import com.southwind.common.TreeNode;
import com.southwind.entity.Admin;
import com.southwind.entity.Menu;
import com.southwind.entity.User;
import com.southwind.mapper.MenuMapper;
import com.southwind.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-05-02
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuMapper menuMapper;

    @RequestMapping("/leftMenu")
    public List<TreeNode> loadIndexLeftMenuJson(){
        Integer type = (Integer) Cache.map.get("type");
        List<Menu> list;
        if(type == 4){
            User user = (User) Cache.map.get("user");
            list = this.menuMapper.getMenuByUserId(user.getId());
        } else {
            Admin admin = (Admin) Cache.map.get("user");
            list = this.menuMapper.getMenuByUserId(admin.getId());
        }
        //将list转成TreeNode
        List<TreeNode> treeNodeList = new ArrayList<>();
        for (Menu menu : list) {
            TreeNode treeNode = new TreeNode();
            BeanUtils.copyProperties(menu, treeNode);
            if(menu.getSpread() == 1) treeNode.setSpread(true);
            treeNodeList.add(treeNode);
        }
        return treeNodeList;
    }

}

